package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.web;


import com.iwhalecloud.citybrain.flow.platform.manager.common.ResponseResult;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.enums.MsgTypeEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.*;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowMessageRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhangnn
 * @date 2020-11-30 15:55
 */

@RestController
@RequestMapping("/message")
public class FlowMessageController {

    @Autowired
    private FlowMessageRepository flowMessageRepository;


    /**
     * 列表
     *
     * @param flowMessageVo 参数
     * @return 结果
     */
    @PostMapping("/list")
    public ResponseResult list(@RequestBody FlowMessageVo flowMessageVo) {
        return flowMessageRepository.list(flowMessageVo);
    }

    /**
     * 详情
     *
     * @param id 参数
     * @return 结果
     */
    @GetMapping("/detail")
    public ResponseResult detail(@RequestParam(value="id") String id) {
        FlowMessageListVo detail = flowMessageRepository.detail(id);
        return ResponseResult.success(detail);
    }

    /**
     * 更新已读状态
     *
     * @param flowMessageUpdateStatusVo 参数
     * @return 结果
     */
    @PostMapping("/updateReadStatus")
    public ResponseResult updateReadStatus(@RequestBody FlowMessageUpdateStatusVo flowMessageUpdateStatusVo) {
        return ResponseResult.success(flowMessageRepository.updateReadStatus(flowMessageUpdateStatusVo));
    }


    /**
     * 消息类型列表
     */
    @GetMapping("/msgtype")
    public ResponseResult getMsgType() {

        Map<String, Map<String, String>> msgTypeMap = MsgTypeEnum.getCodeToDesc();

        List<MsgTypeVo> msgTypeVoList = new ArrayList<>();

        for (Map.Entry<String, Map<String, String>> entry : msgTypeMap.entrySet()) {

            Map<String, String> value = entry.getValue();
            for (String code : value.keySet()) {
                MsgTypeVo msgTypeVo = new MsgTypeVo();
                msgTypeVo.setType(entry.getKey());
                msgTypeVo.setKey(code);
                msgTypeVo.setValue(value.get(code));
                msgTypeVoList.add(msgTypeVo);
            }
        }

        return ResponseResult.success().setDatas(msgTypeVoList);
    }

}
